package com.jsyl.lwbk.business.handle;

import com.jsyl.lwbk.business.domain.*;
import com.jsyl.lwbk.business.mapper.*;
import com.jsyl.lwbk.business.vo.EvaluateModuleVO;
import com.jsyl.lwbk.business.vo.EvaluateProjectVO;
import com.jsyl.lwbk.business.vo.EvaluateSortVO;
import com.jsyl.lwbk.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
public class ReportsHandle {

    @Autowired
    ScaleEvaluateMapper scaleEvaluateMapper;

    @Autowired
    EvaluateRecordMapper evaluateRecordMapper;

    @Autowired
    ChildMapper childMapper;

    @Autowired
    EvaluateScoreMapper evaluateScoreMapper;

    @Autowired
    ScaleProjectMapper scaleProjectMapper;

    @Autowired
    EvaluateReportMapper evaluateReportMapper;

    /**
     * 保存报告
     *
     * @param childId
     */
    public boolean saveReports(Long childId, String evaluateNumber) {
        Child child = childMapper.selectById(childId);
        Map<String, Object> ageMap = DateUtils.toAge(Integer.parseInt(child.getBirthYear()), Integer.parseInt(child.getBirthMonth()));
        Integer ageMonth = (Integer) ageMap.get("ageMonth");
        evaluateModuleReport(evaluateNumber, child, ageMonth);
        projectModuleReport(evaluateNumber, child, ageMonth);
        updateReport(evaluateNumber);
        return true;
    }

    private void updateReport(String evaluateNumber) {
        EvaluateReport report = evaluateReportMapper.findByEvaluateNumber(evaluateNumber);
        report.setStatus(0);
        evaluateReportMapper.insert(report);
    }

    /**
     * 按照测评模块保存
     *
     * @param evaluateNumber
     * @param child
     * @param ageMonth
     */
    private void evaluateModuleReport(String evaluateNumber, Child child, Integer ageMonth) {
        List<EvaluateModuleVO> evaluateModuleVOList = scaleEvaluateMapper.selectEvaluateModule(child.getId());
        for (EvaluateModuleVO evaluateModuleVO : evaluateModuleVOList) {
            EvaluateScore evaluateScore = new EvaluateScore();
            switch (evaluateModuleVO.getScaleType()) {
                case 1:
                    // 计算分数
                    calculateModuleGrade(evaluateNumber, evaluateModuleVO.getEvaluateModule(), ageMonth, evaluateScore, child);
                    break;
                case 2:
                    // 计算次数
                    calculateModuleNum(evaluateNumber, child, ageMonth, evaluateModuleVO.getEvaluateModule(), evaluateScore);
                    break;
                default:
            }
            evaluateScoreMapper.insert(evaluateScore);
        }
    }

    /**
     * 按照课程模块保存总分
     *
     * @param evaluateNumber
     * @param child
     */
    private void projectModuleReport(String evaluateNumber, Child child, Integer ageMonth) {
        List<EvaluateProjectVO> evaluateProjectVOList = scaleProjectMapper.selectEvaluateProject(child.getId());
        for (EvaluateProjectVO evaluateProjectVO : evaluateProjectVOList) {
            EvaluateScore evaluateScore = new EvaluateScore();
            switch (evaluateProjectVO.getScaleType()) {
                case 1:
                    // 计算分数
                    calculateProjectGrade(evaluateNumber, evaluateProjectVO.getEvaluateProject(), ageMonth, evaluateScore, child);
                    break;
                case 2:
                    calculateProjectNum(evaluateNumber, evaluateProjectVO.getEvaluateProject(), ageMonth, evaluateScore, child);
                    break;
                default:

            }
        }
    }

    private void calculateProjectNum(String evaluateNumber, Integer evaluateProject, Integer ageMonth, EvaluateScore evaluateScore, Child child) {
        ScaleProject scaleProject;
        EvaluateSortVO evaluateSortVO = evaluateRecordMapper.selectCountMaxByModule(child.getId(), evaluateNumber, evaluateProject);
        scaleProject = scaleProjectMapper.selectScaleNumByProject(evaluateProject, evaluateSortVO.getChildTotalScore());
        creationEvaluateScore(scaleProject, evaluateScore, child, evaluateSortVO);
    }

    /**
     * 测评项目的计算分数区间
     *
     * @param evaluateNumber
     * @param evaluateProject
     * @param ageMonth
     * @param evaluateScore
     * @param child
     */
    private void calculateProjectGrade(String evaluateNumber, Integer evaluateProject, Integer ageMonth, EvaluateScore evaluateScore, Child child) {
        ScaleProject scaleProject;
        EvaluateSortVO evaluateSortVO = evaluateRecordMapper.selectProjectSortByChildId(child.getId(), evaluateNumber, evaluateProject);
        scaleProject = scaleProjectMapper.selectScaleByProject(evaluateProject, evaluateSortVO.getChildTotalScore());
        creationEvaluateScore(scaleProject, evaluateScore, child, evaluateSortVO);
    }

    /**
     * 按照分数区间、年龄计算，并根据量表生成测评报告对象
     *
     * @param evaluateNumber
     * @param evaluateModule
     * @param ageMonth
     * @param evaluateScore
     * @param child
     */
    private void calculateModuleGrade(String evaluateNumber, Integer evaluateModule, Integer ageMonth, EvaluateScore evaluateScore, Child child) {
        ScaleEvaluate scaleEvaluate;
        EvaluateSortVO evaluateSortVO = evaluateRecordMapper.selectSortByChildId(child.getId(), evaluateNumber, evaluateModule);
        scaleEvaluate = scaleEvaluateMapper.selectScaleByModule(evaluateModule, evaluateSortVO.getChildTotalScore());
        if (scaleEvaluate != null && (scaleEvaluate.getAgeStart() != null || scaleEvaluate.getAgeEnd() != null)) {
            if (!between(ageMonth, scaleEvaluate.getAgeStart(), scaleEvaluate.getAgeEnd())) {
                scaleEvaluate = scaleEvaluateMapper.selectScaleByModule(evaluateSortVO.getEvaluateModule(), null);
            }
        }
        creationEvaluateScore(scaleEvaluate, evaluateScore, child, evaluateSortVO);
    }

    /**
     * 按照次数、年龄计算，并根据量表生成测评报告对象
     *
     * @param evaluateNumber
     * @param child
     * @param ageMonth
     * @param evaluateModule
     * @param evaluateScore
     */
    private void calculateModuleNum(String evaluateNumber, Child child, Integer ageMonth, Integer evaluateModule, EvaluateScore evaluateScore) {
        ScaleEvaluate scaleEvaluate;
        EvaluateSortVO evaluateSortVO = evaluateRecordMapper.selectCountMaxByModule(child.getId(), evaluateNumber, evaluateModule);
        scaleEvaluate = scaleEvaluateMapper.selectScaleNumByModule(evaluateModule, evaluateSortVO.getChildTotalScore());
        if (scaleEvaluate.getAgeStart() != null || scaleEvaluate.getAgeEnd() != null) {
            if (!between(ageMonth, scaleEvaluate.getAgeStart(), scaleEvaluate.getAgeEnd())) {
                scaleEvaluate = scaleEvaluateMapper.selectScaleNumByModule(evaluateModule, null);
            }
        }
        creationEvaluateScore(scaleEvaluate, evaluateScore, child, evaluateSortVO);
    }


    /**
     * 创建测评报告对象
     *
     * @param scaleEvaluate
     * @param evaluateScore
     * @param child
     * @param evaluateSortVO
     */
    private void creationEvaluateScore(ScaleEvaluate scaleEvaluate, EvaluateScore evaluateScore, Child child, EvaluateSortVO evaluateSortVO) {
        evaluateScore.setProjectModule(evaluateSortVO.getProjectModule());
        evaluateScore.setProjectType(evaluateSortVO.getProjectType());
        evaluateScore.setScore(evaluateSortVO.getChildTotalScore());
        evaluateScore.setCenterId(child.getCenterId());
        evaluateScore.setChildId(child.getId());
        evaluateScore.setEvaluateModule(scaleEvaluate.getEvaluateModule());
        evaluateScore.setEvaluateNumber(DateUtils.dateTimeNow());
        evaluateScore.setReportRemark(scaleEvaluate.getRemark());
    }

    private void creationEvaluateScore(ScaleProject scaleEvaluate, EvaluateScore evaluateScore, Child child, EvaluateSortVO evaluateSortVO) {
        evaluateScore.setProjectModule(evaluateSortVO.getProjectModule());
        evaluateScore.setProjectType(evaluateSortVO.getProjectType());
        evaluateScore.setScore(evaluateSortVO.getChildTotalScore());
        evaluateScore.setCenterId(child.getCenterId());
        evaluateScore.setChildId(child.getId());
        evaluateScore.setEvaluateNumber(DateUtils.dateTimeNow());
        evaluateScore.setReportRemark(scaleEvaluate.getRemark());
    }

    /**
     * 判断年龄是否在给定的两个区间
     *
     * @param num
     * @param startNum
     * @param endNum
     * @return
     */
    private boolean between(Integer num, Integer startNum, Integer endNum) {
        boolean flat = false;
        if (num >= startNum && num <= endNum || num >= endNum && num <= startNum) {
            flat = true;
        }
        return flat;
    }

}
